<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head th:include="base/head.html::static">
<title>修改密码</title>
</head>
  
<body>
<div class="lyear-layout-web">
  <div class="lyear-layout-container">
    <!--左侧导航-->
    <aside class="lyear-layout-sidebar" th:include="base/base.html::left"></aside>

    <!--头部信息-->
    <header class="lyear-layout-header" th:include="base/base.html::header"></header>
    
    <!--页面主要内容-->
    <main class="lyear-layout-content">
      
      <div class="container-fluid">
        
        <div class="row">
          <div class="col-lg-12">
            <div class="card">
              <div class="card-body">
                
                <form method="post" action="#!" class="site-form">
                  <div class="form-group">
                    <label for="old-password">旧密码</label>
                    <input type="password" class="form-control" name="oldpwd" id="old-password" placeholder="输入账号的原登录密码">
                  </div>
                  <div class="form-group">
                    <label for="new-password">新密码</label>
                    <input type="password" class="form-control" name="newpwd" id="new-password" placeholder="输入新的密码">
                  </div>
                  <div class="form-group">
                    <label for="confirm-password">确认新密码</label>
                    <input type="password" class="form-control" name="confirmpwd" id="confirm-password" placeholder="请输入正确的邮箱地址">
                  </div>
                  <button type="button" onclick="changePassword()" class="btn btn-primary">修改密码</button>
                </form>
       
              </div>
            </div>
          </div>
          
        </div>
        
      </div>
      
    </main>
    <!--End 页面主要内容-->
  </div>
</div>
</body>
<script th:inline="javascript">
  var changePassword=function () {
    //alert("触发点击事件");
    var oldPwd=$("#old-password").val();
    var newPwd=$("#new-password").val();
    var comfirmPwd=$("#confirm-password").val();
    var userNum=[[${session.user.userNum}]];
   // alert("userNum:"+userNum);
    if(oldPwd =="" || newPwd == "" || comfirmPwd==""){
      alert("输入框不能为空");
    }else {
      if(newPwd == comfirmPwd){
          axios({
            method: 'put',
            url: '/user/password/'+userNum,
            data:{
              oldPwd:oldPwd,
              newPwd:newPwd
            }
          }).then(
                  function (response) {
                    //alert("返回结果："+response.data);
                    if(response.data=="passwordSuccess"){
                      alert("密码修改成功，请重新登录！！")
                      window.location.href="/logout";
                    }
                    if(response.data=="passwordError"){
                      alert("输入的原始密码有误!!");
                      $("#old-password").val("");
                      $("#new-password").val("");
                      $("#confirm-password").val("");
                    }
                  }
          );
      }else {
          alert("两次密码不一致");
          $("#new-password").val("");
          $("#confirm-password").val("");
      }
    }
  }
</script>
</html>